/**
 * Created on : 2015/11/28
 * Author     : @kankan
 */

/************************************************************************************************************************
 ******************************************************** xrs *********************************************************
 ************************************************************************************************************************/
// create xrs object
var xrs = xrs || {};

(function() {
	
    /**************************************************
     * XRS
     **************************************************/
    var XRS = function(obj, options) {
		var self = this;
    	
		self.$obj = obj;								// 加了$只是为了区分Js变量而已，可以用来表示这个变量是一个jquery对象，没其他含义
		self.$options = options;
		self.$elem = null;

		self._init();
    };
    
    XRS.prototype = {
        /* 初始化 */
        _init: function() {
            var self = this;
            
            self._preProcess();
            self._createDomElem();						// create and display
    		
            return self;								// VERY IMPORTANT!!! 这样才能继续调用后面的其它方法
        },
        
        /* 预处理 */
        _preProcess: function() {
            var self = this;
            
            self.$options = $.extend({}, XRS.DEFAULTS, self.$options);		// 将调用者的参数与本对象的默认参数进行合并
        },
        
        /* 构建HTML */
        _createDomElem: function() {
            var self = this;
            
//        	self.$elem = $(self.$obj);
            $(self.$obj).each(function(i) {
//            	console.log(i);
            	var inputObj = $(this);
            	var inputId = '';
            	var labelObj = null;
            	var labelNames = '';
            	var labelNameArray = null;
            	var uncheckedTxt = '';
            	var checkedTxt = '';
    			
    			// process obj
    			inputObj.addClass('xrs-input');
    			
    			// add id for input if input no id
    			if (inputObj.attr('id')) {
    				inputId = inputObj.attr('id');
    			} else {
    				inputId = 'xrs-' + 1 + Math.floor(Math.random() * 1000 * 1000);
        			inputObj.attr('id', inputId);
    			}
    			
    			// label name
    			labelNames = inputObj.attr("data-label");
    			
    			if (self.$options.labelEnable === true) {
    				if (labelNames == null || labelNames.length === 0) {
    					// If attribute has no label and we want to use, then use the default labelNames
    					labelNameArray = [self.$options.uncheckedTxt, self.$options.checkedTxt]
    				} else {
    					// Ok, ok, it's time to split Checked and Unchecked labelNames
    					// We split, by the "self.$options.separator" option
    					labelNameArray = labelNames.split(self.$options.separator);

    					// Now, let's check if exist _only_ two labelNames
    					// If there's more than two, then we do not use labelNames :(
    					// Else, do some additional tests
    					if (labelNameArray.length > 2) {
    						label_enable = false;
    					}
    				}
    			}
    			
    			// labelNameArray ==> uncheckedTxt + checkedTxt
    			if (labelNameArray == null)
    				uncheckedTxt = checkedTxt = "";
    			else {
    				uncheckedTxt = labelNameArray[0];

    				// If checked message is null, then put the same text of unchecked message
    				if (labelNameArray[1] == null)
    					checkedTxt = uncheckedTxt;
    				else
    					checkedTxt = labelNameArray[1];
    			}
    			
    			// labelObj
    			if (self.$options.labelEnable == true && self.$options.iconEnable == true) {
        			labelObj = $('\
            				<label for="' + inputId + '" class="xrs-label ' + self.$options.xRsLabelClass + '">\
            					<span class="xrs-checked"><i class="xrs-label-icon ' + self.$options.checkedIcon + '"></i> <span class="xrs-label-txt">' + checkedTxt + '</span></span>\
            					<span class="xrs-unchecked"><i class="xrs-label-icon ' + self.$options.uncheckedIcon + '"></i> <span class="xrs-label-txt">' + uncheckedTxt + '</span></span>\
        					</label>\
        				');
    			} else if (self.$options.labelEnable == true) {
        			labelObj = $('\
            				<label for="' + inputId + '" class="xrs-label ' + self.$options.xRsLabelClass + '">\
            					<span class="xrs-checked"><span class="xrs-label-txt">' + checkedTxt + '</span></span>\
            					<span class="xrs-unchecked"><span class="xrs-label-txt">' + uncheckedTxt + '</span></span>\
        					</label>\
        				');
    			} else {
        			labelObj = $('\
            				<label for="' + inputId + '" class="xrs-label ' + self.$options.xRsLabelClass + '">\
            					<span class="xrs-checked"><i class="xrs-label-icon ' + self.$options.checkedIcon + '"></i></span>\
            					<span class="xrs-unchecked"><i class="xrs-label-icon ' + self.$options.uncheckedIcon + '"></i></span>\
        					</label>\
        				');
    			}

    			inputObj.after(labelObj);
            });
        },
        
        /* Destroy */
        destroy: function() {
//        	console.log('XRS destroy...');
            var self = this;

			$(self.$obj).removeClass('xrs-input');
            $(self.$obj).siblings('.xrs-label').remove();
            
            return this;
        }
    };

    XRS.DEFAULTS = {
    	xRsLabelClass		: 'xrs-label-default',
    	labelEnable			: true,
    	iconEnable			: false,
    	separator			: "|",
    	checkedTxt			: "Checked",
    	uncheckedTxt		: "Unchecked",
    	checkedIcon			: "fa fa-dot-circle-o",
    	uncheckedIcon		: "fa fa-circle-thin",
    };
    
    /** xrs */
	xrs = function(obj, options) {
		return new XRS(obj, options);
    };
    xrs.defaults = XRS.DEFAULTS;
    
})();